import urlConfig from './config.js'
import tui from './tui.js'
import constant from '../constant/constant.js'
const fetch = {
	// 提示
	toast: function(tips) {
		uni.showToast({
			title: tips || "网络请求失败，请联系管理员",
			icon: 'none',
			duration: 2000
		})
	},
	
	delayed: null,
	loadding: false,
	showLoading: function(title, mask = true) {
		uni.showLoading({
			mask: mask,
			title: title || '请稍候...'
		})
	},
	isUserId:true,
	
	//接口地址
	// interfaceUrl: function() {
	// 	return 'http://bct.sohoht.com'
	// },
	
	/**
	 * 请求数据处理
	 * @param String url 请求地址
	 * @param String method 请求方式 GET or POST
	 * @param {*} postData 请求参数
	 * @param Boolean isForm 数据格式
	 *  true: 'application/x-www-form-urlencoded'
	 *  false:'application/json'
	 * @param Boolean isDelay 是否延迟显示loading
	 * @param Boolean hideLoading 是否隐藏loading
	 *  true: 隐藏
	 *  false:显示
	 */
	request:function (url, method, postData, isForm, isDelay, hideLoading,isUserId=true) {
		//接口请求
		fetch.loadding && uni.hideLoading();
		fetch.loadding = false;
		if (!hideLoading) {
			if (isDelay) {
				fetch.delayed = setTimeout(() => {
					fetch.loadding = true
					fetch.showLoading()
				}, 1000)
			} else {
				fetch.loadding = true
				fetch.showLoading()
			}
		}
		let queryData
		let saleUserId= uni.getStorageSync(constant.StorageName.APP_USER_INFO).id;
		if(isUserId&&saleUserId){
			queryData=Object.assign({},{saleUserId:saleUserId},postData)
		}else{
			queryData=postData
		}
		 
		// 获取token
		let token = uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
		return new Promise((resolve, reject)=>{
			uni.request({
				// #ifdef MP-WEIXIN || APP-PLUS || MP
				url: urlConfig + url,
				// #endif
				// #ifdef H5
				url: urlConfig + url,
				// #endif
				data: queryData, // 请求参数
				header: {
					'content-type': isForm ? 'application/x-www-form-urlencoded' : 'application/json',
					'Authorization':'Bearer ' + token ,
				},
				method: method, //'GET','POST'
				dataType: 'json', // 如果设为 json，会尝试对返回的数据做一次 JSON.parse
				
				success: (res) => {
					clearTimeout(fetch.delayed)
					fetch.delayed = null;
					if (fetch.loadding && !hideLoading) {
						uni.hideLoading()
					}
					if (res.data.code == 0) {
						resolve(res.data)
					}else{
						switch (res.data.code) {
							// case 520:
							// 	uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, "")
							// 	uni.setStorageSync(constant.StorageName.APP_USER_INFO, "")
							// 	tui.toast(res.data.msg)
							// 	setTimeout(()=>{
							// 		uni.reLaunch({
							// 			url: constant.StorageName.APP_LOGIN_PATH
							// 		});
							// 	},800)
							// break;
							case 525:
							uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, "")
							uni.setStorageSync(constant.StorageName.APP_USER_INFO, "")
								tui.toast("请重新登录")
								setTimeout(()=>{
									uni.reLaunch({
										url: constant.StorageName.APP_LOGIN_PATH
									});
								},800)
							break;
							case 401:
							uni.setStorageSync(constant.StorageName.APP_LOGIN_TOKEN, "")
							uni.setStorageSync(constant.StorageName.APP_USER_INFO, "")
								tui.toast("请重新登录")
								setTimeout(()=>{
									uni.reLaunch({
										url: constant.StorageName.APP_LOGIN_PATH
									});
								},800)
							break;
							case 404:
								tui.toast("404请检查接口名称~")
							break;
							default:
								tui.toast(res.data.msg)
								reject(res.data)
							break;
						}
						
					}
				},
				fail: (res) => {
					clearTimeout(fetch.delayed)
					fetch.delayed = null;
					tui.toast("网络不给力，请稍后再试~")
					reject(res)
				}
			})
		})
	},

	
	/**
	 * 上传文件
	 * @param string url 请求地址
	 * @param string src 文件路径
	 */
	uploadFile:function(url, src){
		const that = this
		fetch.showLoading()
		return new Promise((resolve, reject)=>{
			const uploadTask = uni.uploadFile({
				// url: urlConfig+'',
				url: urlConfig + url,
				filePath: src,
				name: 'file',
				header: {
					'content-type': 'multipart/form-data',
					'Authorization': 'Bearer ' + uni.getStorageSync(constant.StorageName.APP_LOGIN_TOKEN)
				},
				formData: {
					// sizeArrayText:""
				},
				success: function(res) {
					uni.hideLoading()
					let d = JSON.parse(res.data)
					if (d.code === 1) {
						let fileObj = JSON.parse(d.data)[0];
						//文件上传成功后把图片路径数据提交到服务器，数据提交成功后，再进行下张图片的上传
						resolve(fileObj)
					} else {
						that.toast(res.message);
					}
				},
				fail: function(res) {
					reject(res)
					uni.hideLoading();
					that.toast(res.message);
				}
			})
		})
	}
}


// module.exports = {
// 	request: fetch.request,
// 	toast: fetch.toast
// }

export default fetch